package com.example.shopping;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping.fpage.FirstPageFragment;
import com.example.shopping.goods.GoodsFragment;
import com.example.shopping.me.LoginActivity;
import com.example.shopping.me.PersonCenterActivity;
import com.example.shopping.me.UserBean;
import com.example.shopping.utils.SaveUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    RadioGroup mainRg;
    List<Fragment> fList;
    FragmentManager fmanager;

    Button syBtn, typeBtn, meBtn, logBtn, regBtn, exitBtn;
    TextView logTv;
    DrawerLayout drawerLayout;
    RadioButton syRb, typeRb, meRb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainRg = findViewById(R.id.main_rg);
        mainRg.setOnCheckedChangeListener(this);//设置监听
        initFrag();
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (judgeLoginStatus()){
            UserBean.DataBean userinfo=SaveUserUtils.getUserinfo();
            String nickname=userinfo.getNickname();
            logTv.setText(nickname+"用户，\n欢迎您!");
        }else {
            logTv.setText("未登录，请先登录！");
        }
    }

    private void initView() {
        syRb = findViewById(R.id.main_rb_fpage);
        typeRb = findViewById(R.id.main_rb_prod);
        meRb = findViewById(R.id.main_rb_me);
        syBtn = findViewById(R.id.main_left_btn_fp);
        meBtn = findViewById(R.id.main_left_btn_me);
        typeBtn = findViewById(R.id.main_left_btn_type);
        logBtn = findViewById(R.id.main_left_btn_login);
        regBtn = findViewById(R.id.main_left_btn_reg);
        exitBtn = findViewById(R.id.main_left_btn_exit);
        logTv = findViewById(R.id.main_left_tv1);
        drawerLayout = findViewById(R.id.main_drawer);
        syBtn.setOnClickListener(this);
        meBtn.setOnClickListener(this);
        typeBtn.setOnClickListener(this);
        logBtn.setOnClickListener(this);
        regBtn.setOnClickListener(this);
        exitBtn.setOnClickListener(this);
    }


    //页面当中相关碎片初始化的工作
    private void initFrag() {
        fList = new ArrayList<>();
        fList.add(new FirstPageFragment());
        fList.add(new GoodsFragment());
        fList.add(new MeFragment());

        fmanager= getSupportFragmentManager();
        FragmentTransaction ft =fmanager.beginTransaction();
        for (int i = 0; i < fList.size(); i++) {
            ft.add(R.id.main_layout, fList.get(i));
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(0));
        ft.commit();
    }

    //切换
    private void changeFrag(int pos) {
        FragmentTransaction ft = fmanager.beginTransaction();
        for (int i = 0; i < fList.size(); i++) {
            ft.hide(fList.get(i));
        }
        ft.show(fList.get(pos));
        ft.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rb_fpage:
                changeFrag(0);
                break;
            case R.id.main_rb_prod:
                changeFrag(1);
                break;
            case R.id.main_rb_me:
                changeFrag(2);
                MeFragment frag=(MeFragment)fList.get(2);
                frag .onResumePage();
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_left_btn_fp:
                syRb.setChecked(true);
                break;
            case R.id.main_left_btn_type:
                typeRb.setChecked(true);
                break;
            case R.id.main_left_btn_me:
                meRb.setChecked(true);
                break;
            case R.id.main_left_btn_login:
               startLoginPage(0);
                break;
            case R.id.main_left_btn_reg:
             startLoginPage(1);
                break;
            case R.id.main_left_btn_exit:
                UserBean.DataBean bean = SaveUserUtils.getUserinfo();
                if (bean != null) {
                    SaveUserUtils.delUserInfo();
                    SaveUserUtils.deleteUserInfo(this, bean);
                    Toast.makeText(this, "注销成功！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "用户未登录！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        drawerLayout.closeDrawers();

    }

    public boolean judgeLoginStatus() {
        UserBean.DataBean bean = SaveUserUtils.getUserinfo();
        if (bean == null) {
            bean = SaveUserUtils.getUserToObj(this);
            if (bean == null) {
                return false;
            } else {
                return true;
            }
        }
        return true;
    }


        //跳转登录界面
        public void startLoginPage ( int flag){
            if (judgeLoginStatus()) {  //已经登录
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示信息").setMessage("用户已登录，是否跳转到个人中心界面").setIcon(R.mipmap.icon);
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(MainActivity.this,PersonCenterActivity.class);
                        startActivity(intent);

                        //跳转
                    }
                });
                builder.create().show();
            } else {  //没有登录
                Intent it = new Intent(this, LoginActivity.class);
                it.putExtra("login", flag);
                startActivity(it);
            }
        }
    }


